<? if (!defined("BASEPATH")) exit("No direct script access allowed");

//Common::pr($getMessage);
$this->load->helper('text');

echo	"<div class=\"accordion\">";
if(empty($getMessage)) {
	echo	"<div class=\"alert_info\">".$this->lang->line("MESSAGE_BOX_IS_EMPTY")."</div>";
}
foreach($getMessage as $uniqueId => $messages) {

	$sender = $getMessage[$uniqueId][count($getMessage[$uniqueId])-1]["mesSenderUsrId"];

	$unread_messages = 0;
	foreach($messages as $message)  {
		if(!$message["mesReaderUsrId"] and $message["mesSenderUsrId"] != $this->session->userdata("usrId")) {
			$unread_messages++;
		}
	}

	echo	"<article class=\"box\">";

	echo		"<header>";
	echo			"<h3>".$messages[0]["mesSubject"]." [".$messages[0]["insName"]."]</h3>";
	echo			"<dl style=\"position:absolute; top:-10px;\">";
	echo				"<dt>".$this->lang->line("NUMBER_OF_MESSAGES").": ".count($messages).(($unread_messages)?(" (".$unread_messages." ".$this->lang->line("NEW").")"):(""))."</dt><br/>";
	echo				"<dt>".$this->lang->line("LAST_MESSAGE_AT").": ".str_replace("-", ".", $messages[0]["mesCreatedate"])."</dt>";
	echo			"</dl>";
	echo		"</header>";

	echo		"<section>";

	echo			"<div class=\"box green\">";
	echo				form_open_multipart("messages", array("id" => "frm_".$uniqueId));
	echo					"<textarea style=\"padding: 0;\" class=\"data w100\" name=\"message\" id=\"message\" placeholder=\"".$this->lang->line("MESSAGE")."\"></textarea><br><br>";
	echo					form_hidden("insId", $messages[0]["mesInsId"]);
	echo					form_hidden("mesId", "");
	echo					form_hidden("uniqueId", $uniqueId);
	echo					"<input type=\"submit\" class=\"button\" value=\"".$this->lang->line("SEND")."\">";
	echo					"<div id=\"upload_".$uniqueId."_toggle\" class=\"fr\">".anchor("#", $this->lang->line("ATTACH_FILE"), "onclick=\"$(this).hide(); $('#upload_".$uniqueId."').show(); return false;\"")."</div>";
	echo					"<div id=\"upload_".$uniqueId."\" class=\"fr\" style=\"display: none;\">".form_upload("Filedata")."</div>";
	echo				form_close();
	echo			"</div>";

	foreach($messages as $message)  {
		echo			"<div class=\"box green\">";

		echo				"<div class=\"boxcontent\">";
		// mailto($message["senderUsrEmail"])
		echo					"<p class=\"".($message["mesSenderUsrId"] == $sender ? "fr" : "fl")."\">".(($message["senderFullname"])?($message["senderFullname"]):($message["mesSenderUsrId"] == $this->session->userdata("usrId") ? $this->lang->line("ME") : substr($message["senderUsrEmail"], 0, strpos($message["senderUsrEmail"], "@"))."@")).(($message["senderIsIns"])?(" (".character_limiter($message["insName"], 10).")"):(""))."</p>";
		echo					"<p class=\"".($message["mesSenderUsrId"] == $sender ? "fl" : "fr")."\">".$message["mesCreatedate"].((!$message["mesReaddate"])?(" (".((($message["mesSenderUsrId"] == $this->session->userdata("usrId"))?(Common::coalesce(array($message["mesReaddate"], $this->lang->line("UNREAD")))):(Common::coalesce(array($message["mesReaddate"], $this->lang->line("NEW")))))).")"):(""))."</p>";
		echo					"<br class=\"cb\" /><br class=\"cb\" />";
		echo					auto_link($message["mesMessage"]);
		if($message["mesFileName"]) {
			echo					"<br/><p class=\"fr\"><img src=\"".MEDIA."icon/attach.png\" alt=\"\" /> ".anchor("messages/download/".$message["mesFileName"], $message["mesFileNameOriginal"])."</p>";
		}
		if($this->session->userdata("usrId") == $message["mesSenderUsrId"]) {
			if(!$message["mesFileName"]) echo					"<br/>";
			echo					"<br/><p class=\"f\">".anchor("#", $this->lang->line("EDIT_MESSAGE"), "onclick=\"\$('#frm_".$uniqueId." :input[name=message]').val('".$this->db->escape_str($message["mesMessage"])."');    \$('html, body').animate({ scrollTop: \$('#frm_".$uniqueId."').offset().top - 150 }, 100); \$('#frm_".$uniqueId." :input[name=mesId]').val('".$message["mesId"]."'); return false;\"")."</p>";
		}
		echo				"</div>";

		echo			"</div>";
	}
	
	echo		"</section>";
	
	echo	"</article>";
	
}
echo	"</div>";